home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14142 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  68 lines

  1. Path: news.nbnet.nb.ca!news
  2. From: lewwid@brunswickmicro.nb.ca (Jeff)
  3. Newsgroups: comp.lang.c
  4. Subject: Can anyone help a newbie out ?
  5. Date: Fri, 12 Apr 1996 02:38:04 GMT
  6. Organization: NBNet
  7. Message-ID: <4kkf3r$5b0@darwin.nbnet.nb.ca>
  8. NNTP-Posting-Host: 198.164.26.48
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. I started reading a book on C, and i am stuck on one exercise, and i
  12. know it's really simple, but i can't do it :(  
  13.  
  14. Problem:Write a function that accepts two strings.  Count the number
  15. of characters in each string, and return the pointer to the longer
  16. string.
  17.  
  18. This is what my lamer brain had to say ...
  19.  
  20. #include <stdio.h>
  21.  
  22. char *ptr, *ptr1;
  23.  
  24. char checksize(char x[], char y[]);
  25.  
  26. main()
  27. {
  28.     /* Prompt for input */
  29.  
  30.     puts("Please enter in the first string :");
  31.     gets(ptr);
  32.     puts("Now the second :");
  33.     gets(ptr1);
  34.  
  35.     /* Print the longest pointer, and call checksize */
  36.  
  37.     printf("The char array that was longer is : %s", checksize(ptr,
  38. ptr1));
  39.  
  40.     return 0;
  41. }
  42. /* Checksize .. checks to see which array of char is longer, and
  43. returns the longest of the 2 */
  44. char checksize(char x[], char y[])
  45. {
  46.     int count, int total = 0, int total1 = 0;
  47.  
  48.     while (*x != NULL)
  49.         total += 1
  50.  
  51.     while (*y != NULL)
  52.         total1 += 1
  53.     
  54.     if (total < total2)
  55.         return *y;
  56.     if (total > total2)
  57.         return *x;
  58. }
  59.  
  60.  
  61.     Yes i know, this is messy and very bad, but i am just starting out :)
  62.  
  63.     If someone could email me with any help, and good pointers, that would
  64. be greatly appreciated ..
  65.  
  66.     lewwid@brunswickmicro.nb.ca
  67.  
  68.